package cn.llming.auth.service.impl;

import cn.llming.auth.domain.Role;
import cn.llming.auth.mapper.RoleMapper;
import cn.llming.auth.query.RoleQuery;
import cn.llming.auth.service.IRoleService;
import cn.llming.basic.query.BaseQuery;
import cn.llming.basic.service.impl.BaseServiceImpl;
import cn.llming.dto.MenuDto;
import cn.llming.dto.RolePermissionDto;
import cn.llming.vo.result.ListPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void delete(Serializable id) {
        super.delete(id);
        //删除中间表中的残留菜单数据
        roleMapper.deleteBeforeMenu((Long)id);
    }
    //删除角色的时候，再删除对应角色与菜单表（z中间表）残留数据
    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        super.batchDelete(ids);
        for (Long id: ids
             ) {
            roleMapper.deleteBeforeMenu(id);
        }
    }
    @Override
    @Transactional
    public void handlePermission(RolePermissionDto dto) {

        //查询对应的角色id中sn，进行删除
        roleMapper.removeRoleId(dto.getRoleId());

        //然后在插入新权限
        roleMapper.patchInsertPermission(dto.getRoleId(),dto.getPermissionSns());
    }

    @Override
    public List<String> getPer(Long dto) {
        return roleMapper.getPer(dto);
    }

    @Override
    public List<String> getRoleIdLoadMenu(Long id) {
        return roleMapper.getRoleIdLoadMenu(id);
    }

    @Override
    @Transactional
    public void saveMenu(MenuDto menuDto) {
        //先删除对应的角色菜单资源
        roleMapper.deleteBeforeMenu(menuDto.getRoleId());
        roleMapper.saveMenu(menuDto.getRoleId(),menuDto.getMenuIds());
    }

    @Override
    @Transactional
    public void saveEmpRoleMenu(MenuDto menuDto) {

        //删除对应员工角色
        roleMapper.deleteEmpBeforRole(menuDto.getRoleId());

        roleMapper.insertNewEmpRole(menuDto.getRoleId(),menuDto.getMenuIds());

    }

    @Override
    public List<Long> getEmpBeforeRole(Long id) {
        return roleMapper.getEmpBeforeRole(id);
    }
}
